/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: e2e_error_domain.h
 *
 * Purpose: 
 *
 * Developer:
 *   wen.gu , 2020-04-21
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_COM_E2E_E2E_ERROR_DOMAIN_H__
#define __ARA_COM_E2E_E2E_ERROR_DOMAIN_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include "ara/core/error_domain.h"
#include "ara/core/exception.h"
#include "ara/core/error_code.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace com
{
namespace e2e
{
enum class E2EError : ara::core::ErrorDomain::CodeType
{
    repeated = 1, /** Data has a repeated counter */

    /** The checks of the Data in this cycle were
     *  successful, with the exception of counter jump,
     *  which changed more than the allowed delta. Error
     *  not related to counters occurred (e.g. wrong crc,
     *  wrong length, wrong Data ID) or the return of the
     *  check function was not OK 
     **/
    wrong_sequence_error = 2,
    error = 3, /** todo, refine me?? */
    /** No value has been received yet (e.g. during
     *  initialization). This is used as the initialization value
     *  for the buffer, it is not returned by any E2E profile.
     **/
    not_available = 4,

    no_new_data = 5,  /**No new data is available. */
};
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

class E2EException : public ara::core::Exception
{
public:
    E2EException(ara::core::ErrorCode err)
        :ara::core::Exception(err)
    {
        /** todo something */
    }
};

class E2EErrorDomain : public ara::core::ErrorDomain
{
    using Errc = E2EError;
    using Exception = E2EException;
public:
    constexpr E2EErrorDomain(void) noexcept
        :ErrorDomain(0x8000000000000400ULL) /** todo, refine me?? */
    {
        /** todo something */
    }

    ~E2EErrorDomain() = default;
public:
    char const* Name() const noexcept override { return "E2E"; }
    char const* Message(ErrorDomain::CodeType errorCode) const noexcept override
    {
        char const* ret = "";
        Errc err = static_cast<Errc>(errorCode);
        switch (err)
        {
        ERR_DOMAIN_CASE(Errc::repeated);
        ERR_DOMAIN_CASE(Errc::wrong_sequence_error); 
        ERR_DOMAIN_CASE(Errc::error);
        ERR_DOMAIN_CASE(Errc::not_available);
        ERR_DOMAIN_CASE(Errc::no_new_data);
        default: break;
        }

        return ret;
    }
    void ThrowAsException(ara::core::ErrorCode const& errorCode) const noexcept(false) override
    {
        throw E2EException(errorCode);
    }

public:
    static E2EErrorDomain const& GetInstance()
    {
        static E2EErrorDomain cem;
        return cem;
    }

    constexpr E2EErrorDomain const& GetRef() const { return  *this; }
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

constexpr inline ara::core::ErrorDomain const& GetCoreErrorDomain() noexcept
{
    return E2EErrorDomain::GetInstance().GetRef();
}

constexpr inline ara::core::ErrorCode MakeErrorCode(E2EError code, ara::core::ErrorDomain::SupportDataType data) noexcept
{
    return std::move(ara::core::ErrorCode(static_cast<ara::core::ErrorDomain::CodeType>(code), GetCoreErrorDomain(), data));
}

} /** namespace e2e */
} /** namespace com */
} /** namespace ara */

#endif /** !__ARA_COM_E2E_E2E_ERROR_DOMAIN_H__ */

